package com.wc.算法提高课.E第五章_数学知识.组合计数.方程的解;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/10/7 0:31
 * @description https://www.acwing.com/problem/content/description/1310/
 */
public class Main {
    /**
     * 思路：隔板法
     * 详细做法见
     *
     * @see com.wc.算法提高课.E第五章_数学知识.容斥原理.Devu和鲜花;
     * C(n, m) = n! / (m! * (n - m)!)
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int P = 1000;
    static int x, k;

    public static void main(String[] args) {
        k = sc.nextInt();
        x = sc.nextInt();
        int gx = qkm(x, x, P);
        out.println(C(gx - 1, k - 1));
        out.flush();
    }

    static BigInteger C(int n, int m) {
        return mmul(n).divide(mmul(m).multiply(mmul(n - m)));
    }

    // 表示x的阶乘
    static BigInteger mmul(int x) {
        BigInteger res = new BigInteger("1");
        for (int i = 1; i <= x; i++) {
            res = res.multiply(new BigInteger(String.valueOf(i)));
        }
        return res;
    }

    static int qkm(int a, int b, int p) {
        a %= 1000;
        int res = 1;
        while (b > 0) {
            if ((b & 1) == 1) res = res * a % p;
            a = a * a % p;
            b >>= 1;
        }
        return res;
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
